﻿using SignalProcessLib.Model.Enum;
using System;
using System.Collections.Generic;
using System.Text;

namespace SignalProcessLib
{
    public class AverageTool
    {
        /// <summary>
        /// 平均方法：将多组频谱数据（等长度），合成一组频谱数据
        /// </summary>
        /// <param name="dataList">多组待平均的频谱数据</param>
        /// <param name="averageType">平均的类型</param>
        /// <returns></returns>
        public static float[] Average(List<float[]> dataList, AverageType averageType = AverageType.Linear)
        {
            try
            {
                int count = dataList.Count;
                float[] res = new float[dataList[0].Length];
                switch (averageType)
                {
                    case AverageType.Linear://线性平均
                        for (int i = 0; i < res.Length; i++)
                        {
                            double temp = 0;
                            for (int j = 0; j < count; j++)
                            {
                                temp += dataList[j][i];
                            }
                            res[i] = (float)(temp / count);
                        }
                        break;

                    case AverageType.LinearAmplitude://稳态平均
                        for (int i = 0; i < res.Length; i++)
                        {
                            float amp = 0;
                            for (int j = 0; j < count; j++)
                            {
                                amp=(count-1)*amp/count + dataList[j][i]/count;
                            }
                            res[i] = amp;
                        }
                        break;

                    case AverageType.Exponential://指数平均
                        int t=1;//常数具体值未知
                        for (int i = 0; i < res.Length; i++)
                        {
                            float amp = 0;
                            for (int j = 0; j < count; j++)
                            {
                                amp = (t- 1) * amp / t + dataList[j][i] / t;
                            }
                            res[i] = amp;
                        }
                        break;

                    case AverageType.PeakHold://峰值电平保持
                        for (int i = 0; i < res.Length; i++)
                        { 
                            float tmp = 0;
                            float hold = dataList[0][i];
                            for (int j = 0; j < count; j++)
                            {
                                if (dataList[j][i] > hold) { hold = dataList[j][i]; }
                               
                            }
                            res[i] = hold;
                        }
                        break;



                    default:
                        break;
                }
                return dataList[0];
            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}
